home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 4133 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.5 KB

  1. Path: hubcap.clemson.edu!hubcap!mjs
  2. From: mjs@hubcap.clemson.edu (M. J. Saltzman)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Float calculations
  5. Date: 1 Feb 96 21:41:18 GMT
  6. Organization: Clemson University
  7. Message-ID: <mjs.823210878@hubcap>
  8. References: <4eqssf$d9q@camelot.ccs.neu.edu>
  9. NNTP-Posting-Host: hubcap.clemson.edu
  10.  
  11. jason@ccs.neu.edu (Jason Leatherman) writes:
  12.  
  13.  
  14. >Hi,
  15.  
  16. >  Check out the results I get when running this simple program on a
  17. >Sparc/UNIX system, compiled with gcc:
  18.  
  19. >#include <stdio.h>
  20.  
  21. >void main()
  22.  
  23. That's "int main()" to you.
  24.  
  25. >{
  26. >  float a, b;
  27.  
  28. >  printf("%0.10f  %0.10f  %0.10f\n", 99974.0, 50.0, 99974.0/50.0);
  29.  
  30. Floating-point constants that are not suffixed with 'f' are doubles,
  31. so the result of the division is a double.
  32.  
  33. >  a = 99974.0;
  34. >  b = 50.0;
  35. >  printf("%0.10f  %0.10f  %0.10f\n", a, b, a/b);
  36.  
  37. Both a and b are float, so the result of the division a float, which is
  38. then promoted to double to pass to printf().
  39.  
  40. Don't forget to 
  41.  
  42.    return 0;
  43.  
  44. >}
  45.  
  46. >The output is:
  47. >99974.0000000000  50.0000000000  1999.4800000000
  48. >99974.0000000000  50.0000000000  1999.4799804688
  49.  
  50. >  Why do the divisions produce different results?  This is probably some
  51. >simple thing that I've forgotten, but I haven't figured it out yet.  Does
  52. >anyone know?  Note that compiling with the -ffloat-store flag didn't make
  53. >a difference.
  54.  
  55. It's not immediately obvious that it should make a difference.
  56.  
  57. >  Thanks for any help,
  58.  
  59. >Jason
  60. -- 
  61.         Matthew Saltzman
  62.         Clemson University Math Sciences
  63.         mjs@clemson.edu
  64.